package com.biodiv.community.controller.post;


import com.biodiv.community.request.post.PostDTO;
import com.biodiv.community.response.post.PostVO;
import com.biodiv.community.base.ResponseEntity;
import com.biodiv.community.service.post.PostService;
import com.biodiv.community.base.PageResult;
import com.biodiv.community.base.PageDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/post")
@Tag(name = "帖子相关接口")
public class PostController {

    @Autowired
    private PostService postService;


    @PostMapping("/add")
    @Operation(summary = "新增帖子")
    public ResponseEntity<Object> addPost(@RequestBody @Validated PostDTO postDTO) {
        postService.addPost(postDTO);
        return ResponseEntity.success();
    }



    @DeleteMapping("/{id}")
    @Operation(summary = "删除帖子")
    public ResponseEntity<Object> deletePost(@PathVariable Integer id) {
        postService.deletePost(id);
        return ResponseEntity.success();
    }


    @GetMapping("/{id}")
    @Operation(summary = "根据id查询帖子")
    public ResponseEntity<PostVO> getPostById(@PathVariable Integer id) {
        PostVO post = postService.getPostById(id);
        return ResponseEntity.success(post);
    }




    @PutMapping("/{id}")
    @Operation(summary = "修改帖子")
    public ResponseEntity<Object> updatePost(@PathVariable Long id, @RequestBody @Validated PostDTO postDTO) {
        postService.updatePost(id,postDTO);
        return ResponseEntity.success();
    }



    @GetMapping("/list")
    @Operation(summary = "分页查询帖子")
    public ResponseEntity<PageResult<PostVO>> postList(PageDTO pageDTO) {
        PageResult<PostVO> postVOPageResponse = postService.postList(pageDTO);
        return ResponseEntity.success(postVOPageResponse);
    }

}
